package cn.fy.sqlite.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import java.util.ArrayList;

import cn.fy.sqlite.R;
import cn.fy.sqlite.activity.SqliteActivity;
import cn.fy.sqlite.dao.UserDao;
import cn.fy.sqlite.model.User;

public class UserAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;

    private UserDao userDao;

    ArrayList<User> userList;
    public UserAdapter(Context context, ArrayList<User> userList) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);

        this.userList = userList;

        userDao = new UserDao(context);
    }

    @Override
    public int getCount() {
        return userList!=null ? userList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return userList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = layoutInflater.inflate(R.layout.layout_user_list_item, null);

        TextView id = view.findViewById(R.id.txt_id);
        TextView name = view.findViewById(R.id.txt_name);
        TextView age = view.findViewById(R.id.txt_age);

        User user = (User)getItem(i);
        id.setText(user.getId() + "");
        name.setText(user.getName());
        age.setText(user.getAge() + "");

        Button b1 = view.findViewById(R.id.btn_update_user);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder customizeDialog = new AlertDialog.Builder(context);
                final View dialogView = LayoutInflater.from(context).inflate(R.layout.layout_user_dialog, null);
                EditText name1 = (EditText) dialogView.findViewById(R.id.txt_dialog_name);
                name1.setText(user.getName());
                EditText age1 = (EditText) dialogView.findViewById(R.id.txt_dialog_age);
                age1.setText(user.getAge() + "");

                customizeDialog.setTitle("修改用户");
                customizeDialog.setView(dialogView);
                customizeDialog.setNegativeButton("取消", null);
                customizeDialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取EditView中的输入内容
                        EditText name = (EditText) dialogView.findViewById(R.id.txt_dialog_name);
                        EditText age = (EditText) dialogView.findViewById(R.id.txt_dialog_age);

                        User u = new User();
                        u.setId(user.getId());
                        u.setName(name.getText().toString());
                        u.setAge(Integer.parseInt(age.getText().toString()));

                        userDao.updateUser(u);

                        ((SqliteActivity)context).refreshListView();
                        Toast.makeText(context, "修改用户成功", Toast.LENGTH_SHORT).show();
                    }
                });
                customizeDialog.show();
            }
        });

        Button b2 = view.findViewById(R.id.btn_del_user);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final AlertDialog.Builder normalDialog =  new AlertDialog.Builder(context);
                normalDialog.setTitle("删除用户");
                normalDialog.setMessage("请确认"  );
                normalDialog.setPositiveButton("确定",  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        userDao.delUser(user);

                        ((SqliteActivity)context).refreshListView();
                        Toast.makeText(context, "删除用户成功", Toast.LENGTH_SHORT).show();
                    }
                });
                normalDialog.setNegativeButton("取消",  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                // 显示
                normalDialog.show();
            }
        });

        return view;
    }
}
